package com.ruoyi.system.service.impl;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.YzmStateType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.constant.NumConstants;
import com.ruoyi.system.domain.bo.YanzhengmaBo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SgXmYanzhengmaMapper;
import com.ruoyi.system.domain.SgXmYanzhengma;
import com.ruoyi.system.service.ISgXmYanzhengmaService;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-20
 */
@Service
public class SgXmYanzhengmaServiceImpl implements ISgXmYanzhengmaService 
{
    @Autowired
    private SgXmYanzhengmaMapper sgXmYanzhengmaMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SgXmYanzhengma selectSgXmYanzhengmaById(Long id)
    {
        return sgXmYanzhengmaMapper.selectSgXmYanzhengmaById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param sgXmYanzhengma 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SgXmYanzhengma> selectSgXmYanzhengmaList(SgXmYanzhengma sgXmYanzhengma)
    {
        return sgXmYanzhengmaMapper.selectSgXmYanzhengmaList(sgXmYanzhengma);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param sgXmYanzhengma 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSgXmYanzhengma(SgXmYanzhengma sgXmYanzhengma)
    {
        return sgXmYanzhengmaMapper.insertSgXmYanzhengma(sgXmYanzhengma);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param sgXmYanzhengma 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSgXmYanzhengma(SgXmYanzhengma sgXmYanzhengma)
    {
        return sgXmYanzhengmaMapper.updateSgXmYanzhengma(sgXmYanzhengma);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSgXmYanzhengmaByIds(Long[] ids)
    {
        return sgXmYanzhengmaMapper.deleteSgXmYanzhengmaByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSgXmYanzhengmaById(Long id)
    {
        return sgXmYanzhengmaMapper.deleteSgXmYanzhengmaById(id);
    }


    private void copyYzm(YanzhengmaBo bo,SgXmYanzhengma yzm){
        yzm.setKeyworks(bo.getKeyworks());
        yzm.setCtype(bo.getCtype());
        yzm.setZb(bo.getZb());
        yzm.setFilesize(bo.getImgsize());
        yzm.setGamearea(bo.getGamearea());
        yzm.setCreatetime(LocalDateTime.now());
        if(!StringUtils.isBlank(bo.getPlayerid())){
            yzm.setPlayerid(bo.getPlayerid());
        }
    }

    @Override
    public boolean updateToImg(YanzhengmaBo bo) {
        SgXmYanzhengma yzm=sgXmYanzhengmaMapper.selectBySize(bo);
        String username = SecurityUtils.getUsername();
        if(yzm!=null){
            yzm.setUsername(username);
            copyYzm(bo,yzm);
            yzm.setState(NumConstants.ZERO);
            return sgXmYanzhengmaMapper.updateSgXmYanzhengma(yzm)>NumConstants.ZERO?true:false;
        }else{
            //添加新的验证码
            yzm=new SgXmYanzhengma();
            yzm.setUsername(username);
            copyYzm(bo,yzm);
            if(bo.getState()!=null && bo.getState()==NumConstants.ONE){
                yzm.setUcount(yzm.getUcount()+NumConstants.ONE);
            }
            yzm.setImgurl(bo.getImgurl());
            yzm.setState(NumConstants.ZERO);
            //yzm.setCreatetime(LocalDateTime.now());
            return sgXmYanzhengmaMapper.insertSgXmYanzhengma(yzm)>NumConstants.ZERO?true:false;
        }
    }

    @Override
    public String getYZMZB(YanzhengmaBo bo) {
        SgXmYanzhengma yzm = sgXmYanzhengmaMapper.selectBySize(bo);
        if (yzm != null) {
            return yzm.getZb();
        }
        return null;
    }

    @Override
    public int addYZM(YanzhengmaBo bo) {
        //检查如果验证码已存在则不在添加
        YanzhengmaBo seach=new YanzhengmaBo();
        seach.setImgsize(bo.getImgsize());
        seach.setImgurl(bo.getImgurl());
        seach.setKeyworks(bo.getKeyworks());
        SgXmYanzhengma sgXmYanzhengma = sgXmYanzhengmaMapper.selectBySize(seach);
        if(sgXmYanzhengma!=null){
            return 1;
        }
        return updateToImg(bo) ? 1 : 0;
    }
}
